﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class HoatDongKHKT
    {
        public String ID;
        public String MaHoatDongKHKT;
        public String MaQuaTrinhHoatDongKHKT;
        public String MaHoiNghiKHKT;
        public String MaKhenThuong;

        public List<QuaTrinhHoatDongKHKT> DSQuaTrinhHoatDongKHKT
        {
            get
            {
                return QuaTrinhHoatDongKHKT.getAllQuaTrinhHoatDongKHKT(MaQuaTrinhHoatDongKHKT);
            }

            set
            {
                DSQuaTrinhHoatDongKHKT = value;
            }
        }

        public List<HoiNghiKHKT> DSHoiNghiKHKT
        {
            get
            {
                return HoiNghiKHKT.getAllHoiNghiKHKT(MaHoiNghiKHKT);
            }

            set
            {
                DSHoiNghiKHKT = value;
            }
        }

        public List<KhenThuong> DSKhenThuong
        {
            get
            {
                return KhenThuong.getAllKhenThuong(MaKhenThuong);
            }
            set
            {
                DSKhenThuong = value;
            }
        }

        public HoatDongKHKT(String _ID)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM HoatDongKHKT " +
                "WHERE ID = '" + _ID + "'");
            if (result.ReturnCode == 200)
            {
                DataRow row = result.Table.Rows[0];
                ID = _ID;
                MaHoatDongKHKT = row["MaHoatDongKHKT"].ToString().ToString();
                MaQuaTrinhHoatDongKHKT = row["MaQuaTrinhHoatDongKHKT"].ToString();
                MaHoiNghiKHKT = row["MaHoiNghiKHKT"].ToString();
                MaKhenThuong = row["MaKhenThuong"].ToString();
            }
            else
                MessageBox.Show(result.Error);
        }

        public HoatDongKHKT(String _MaHoatDongKHKT, QuaTrinhHoatDongKHKT _QuaTrinhHoatDongKHKT, 
            HoiNghiKHKT _HoiNghiKHKT, KhenThuong _KhenThuong)
        {
            ID = Database.getID("HoatDongKHKT", "ID");
            MaHoatDongKHKT = _MaHoatDongKHKT;
            MaQuaTrinhHoatDongKHKT = _QuaTrinhHoatDongKHKT.MaQuaTrinhHoatDongKHKT;
            MaHoiNghiKHKT = _HoiNghiKHKT.MaHoiNghiKHKT;
            MaKhenThuong = _KhenThuong.MaKhenThuong;
        }

        public Boolean Save()
        {
            QueryResult result;
            if (Database.isDupplicate("HoatDongKHKT", "ID", ID))
            {
                //Update
                result = Database.shareDatabase.Query("UPDATE HoatDongKHKT SET " +
                    "MaHoatDongKHKT = '" + MaHoatDongKHKT + "', " +
                    "MaQuaTrinhHoatDongKHKT = '" + MaQuaTrinhHoatDongKHKT + "', " +
                    "MaHoiNghiKHKT = '" + MaHoiNghiKHKT + "', " +
                    "MaKhenThuong = '" + MaKhenThuong + "' "+
                    "WHERE ID = '" + ID + "'");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
            else
            {
                //Insert
                result = Database.shareDatabase.Query("INSERT INTO HoatDongKHKT VALUES(" +
                    "'" + ID + "', " +
                    "'" + MaHoatDongKHKT + "', " +
                    "'" + MaQuaTrinhHoatDongKHKT + "', " +
                    "'" + MaHoiNghiKHKT + "', " +
                    "'" + MaKhenThuong + "')");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
        }

        public Boolean Delete()
        {
            foreach (QuaTrinhHoatDongKHKT qthdkhkt in DSQuaTrinhHoatDongKHKT)
                qthdkhkt.Delete();
            foreach (HoiNghiKHKT hnkhkt in DSHoiNghiKHKT)
                hnkhkt.Delete();
            foreach (KhenThuong kt in DSKhenThuong)
                kt.Delete();

            QueryResult result = Database.shareDatabase.Query("DELETE FROM HoatDongKHKT " +
                "WHERE ID = '" + ID + "'");
            if (result.ReturnCode == 200)
                return true;
            else
                return false;
        }

        public static List<HoatDongKHKT> getAllHoatDongKHKT(String _MaHoatDongKHKT)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM HoatDongKHKT " +
                "WHERE MaHoatDongKHKT = '" + _MaHoatDongKHKT + "'");
            if (result.ReturnCode == 200)
            {
                List<HoatDongKHKT> _result = new List<HoatDongKHKT>();
                foreach (DataRow dtr in result.Table.Rows)
                    _result.Add(new HoatDongKHKT(dtr["ID"].ToString()));
                return _result;
            }
            else
                return null;
        }
    }
}
